
#ifndef GLWIDGET_H
#define GLWIDGET_H

#include <QOpenGLWidget>
#include <QOpenGLFunctions>
#include <QOpenGLShaderProgram>
#include <QOpenGLBuffer>
#include <QMouseEvent>
#include <QWheelEvent>
#include <QVector2D>
#include <QVector3D>
#include <QMatrix4x4>
#include <QDebug>
#include <cmath>

class GLWidget : public QOpenGLWidget, protected QOpenGLFunctions
{
    Q_OBJECT

public:
    GLWidget(QWidget *parent = nullptr);

protected:
    void initializeGL() override;

    void resizeGL(int w, int h) override;

    void paintGL() override;

    void mouseMoveEvent(QMouseEvent *event) override;

    void mousePressEvent(QMouseEvent *event) override;

    void wheelEvent(QWheelEvent *event) override;

private:
    void updateProjection();

    void drawRulers();

    void drawMouseIndicators();

    void drawLine(float x1, float y1, float x2, float y2, const QVector4D& color);

    void drawRect(float x, float y, float width, float height, const QVector4D& color);

private:
    QOpenGLShaderProgram *m_program = nullptr;
    QOpenGLBuffer m_vbo{QOpenGLBuffer::VertexBuffer};
    QMatrix4x4 m_projection;
    QPointF m_mousePos;
    QPointF m_lastMousePos;

    // 状态变量
    float m_offsetX = 0.0f;      // 水平偏移量（厘米）
    float m_scale = 80.0f;       // 缩放比例（像素/厘米）
    int m_width = 800;
    int m_height = 600;
};
#endif // GLWIDGET_H